package gnss

import (
	"fmt"
	"time"
)

func BuildSourceTableBuffer(sourceTable string) []byte {
	src := "SOURCETABLE 200 OK\r\n"
	src += "Server: POP_GW_Ntrip_1.0_1571317283/1.0\r\n"
	src += "Via: n24_131"
	src += "Content-Type: text/plain; charset=UTF-8\r\n"
	src += "Connection: close\r\n"
	l := len(sourceTable)
	src += fmt.Sprintf("Content-Length: %d\r\n", l)
	src += "\r\n"
	src += sourceTable
	src += "ENDSOURCETABLE"
	return []byte(src)
}

//Server: POP_GW_Ntrip_1.0_1571317283/1.0
//Via: n24_131
//Date: 2019/10/26 11:10:12
//Content-Type: text/plain; charset=UTF-8
//Content-Length: 390
//Connection: close

func BuildSourceTableText(sourceTable string, server string) string {
	src := "SOURCETABLE 200 OK\r\n"
	src += fmt.Sprintf("Server: %s\r\n", server)
	src += "Content-Type: text/plain\r\n"
	l := len(sourceTable)
	src += fmt.Sprintf("Content-Length: %d\r\n", l)
	src += "\r\n"
	src += sourceTable
	src += "ENDSOURCETABLE"
	return src
}

/*
*

	nmeaflag: 是否需要发送NMEA。0—不需要 1—需要
	auth :N, B, D
*/
func BuildMPSrcText(mp string, nmeaflag int, auth string) string {
	return fmt.Sprintf("STR;%s;%s;UNKOWN;UNKOWN;0;UNKOWN;misc;CHN;0;0;%d;1;;none;%s;N;0;;;", mp, mp, nmeaflag, auth)
}

func BuildSourceTableTextWithDate(sourceTable string, server string) string {
	l := len(sourceTable)
	src := "SOURCETABLE 200 OK\r\n"
	src += fmt.Sprintf("Server: %s\r\n", server)
	src += fmt.Sprintf("Date: %s \r\n", time.Now().Format("2006/01/02 15:04:05"))
	src += "Content-Type: text/plain; charset=UTF-8\r\n"
	src += fmt.Sprintf("Content-Length: %d\r\n", l)
	src += fmt.Sprintf("Connection: close\r\n")
	src += "\r\n"
	src += sourceTable
	src += "ENDSOURCETABLE"
	return src
}

func BuildUnauthorized(server string) string {
	src := fmt.Sprintf("%s\r\n", NTRIP_UNAUTHORIZED)
	src += fmt.Sprintf("Server: %s\r\n", server)
	src += fmt.Sprintf("Date: %s \r\n", time.Now().Format("2006/01/02 15:04:05"))
	src += "Content-Type: text/plain; charset=UTF-8\r\n"
	src += fmt.Sprintf("Connection: close\r\n")
	src += "\r\n"
	return src
}
